﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Future.Host
{
    public partial class MainForm : Form
    {
        bool _isClose = false;

        public MainForm()
        {
            InitializeComponent();
            this.notifyIcon.Visible = true;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            textBox_ip.Text = Future.Management.Environment.GetLocalIPAddress();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_isClose)
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible)
            {
                this.Hide();
            }
            else
            {
                this.Show();
                this.Activate();
            }
        }

        private void button_Start_Click(object sender, EventArgs e)
        {
            try
            {
                XProcess.Start(textBox_ip.Text.Trim(), textBox_port.Text.Trim());
                label_state.Text = "Start Success...";
                button_Start.Enabled = false;
            }
            catch (Exception ex)
            {
                label_state.Text = "Fail Start. Exception Message" + ex.Message;
            }
        }

        private void button_Stop_Click(object sender, EventArgs e)
        {
            CloseFrom();
        }

        private void CloseFrom()
        {
            this._isClose = true;
            XProcess.Stop();
            this.Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseFrom();
        }
    }
}
